<template>
    <a-drawer
        :destroyOnClose="true"
        :title="title"
        :placement="placement"
        :closable="closable"
        :visible="visible"
        :maskClosable="maskClosable"
        :width="width"
        @close="cancel">

        <a-spin :spinning="confirmLoading" tip="Loading...">
            <LForm ref="leaveTestForm" @onClick="emitClickHandler" @onChange="emitChangeHandler" :formAttributes="formAttributes" :mostColumns="mostColumns" :labelCol="labelCol" :wrapperCol="wrapperCol"></LForm>
        </a-spin>

        <div class="drawer-bootom-button">
            <a-popconfirm title="确定放弃操作？" @confirm="cancel" okText="确定" cancelText="取消">
                <a-button shape="round" @click="cancel" style="margin-right: .8rem">关闭</a-button>
            </a-popconfirm>
            <a-button shape="round" @click="saveData" type="primary" >保存</a-button>
            
            <a-button shape="round" @click="commitData" type="primary" >提交</a-button>
        </div>

        <!--<commit-modal ref="addLeaveCommit" @handleOk="commitForm"></commit-modal>-->
    </a-drawer>
</template>

<script>
import request from '@/utils/flowRequest'
import LForm from '@/components/lt/form/LForm'
import commitModal from '@/views/flowable/view/commitModal'
import pick from 'lodash.pick'
import notification from 'ant-design-vue/es/notification'

export default {
    name: 'leaveTestForm',
    components: { LForm, commitModal },
    props:{
        title:String,
        placement:{
            type:String,
            default:'right'
        },
        closable:{
            type:Boolean,
            default:true
        },
        maskClosable:{
            type:Boolean,
            default:true
        }
    },
    data() {
        return {
            request,
            url: {
                saveForm: '/leaveInfo/saveForm',
                commitForm: '/leaveInfo/commitForm'
            },
            model: {},
            visible: false,
            confirmLoading: false,
            width: 800,
            mostColumns: 1,
            labelCol: {sm: { span: 6 }},
            wrapperCol: {sm: { span: 18 }},
            formAttributes: {
                type:'',
                formItems: [
                    {
                        title:'请假申请',
                        columns:1,
                        fields:[
                            {field:'userId',label:'请假人编号',component:'input',rules: [{required: true, message: '请假人编号不能为空'}]},
                            {field:'userName',label:'请假人姓名',component:'input',rules: [{required: true, message: '请假人编号不能为空'}]},
                            {field:'applyTime',label:'申请时间',component:'time', disabled:true},
                            {field:'leaveNum',label:'请假天数',component:'input', rules: [{required: true, message: '请假天数不能为空'}]},
                            {field:'startTime',label:'请假开始时间',component:'time',rules: [{required: true, message: '请假开始时间不能为空'}]},
                            {field:'endTime',label:'请假结束时间',component:'time',rules: [{required: true, message: '请假结束时间不能为空'}]},
                            {field:'leaveReason',label:'请假原因',component:'textarea',rules: [{required: true, message: '请假原因不能为空'}]},
                            /*
                            {field:'approvalOpinion',label:'审批记录',component:'textarea', disabled:true},
                            */
                        ]
                    }
                ]
            },
        }
    },
    methods: {
        open(data) {
            this.visible = true
            if (data) {
                this.model = data
                this.$nextTick(() => {
                    this.$refs.leaveTestForm.getForm().setFieldsValue(pick(this.model, 'userId','userName', 'applyTime', 'leaveNum','leaveReason','startTime','endTime'))
                })
            }
        },
        commitData() {
            this.$refs.leaveTestForm.getForm().validateFields((err, values) => {
                if (!err) {
                    this.confirmLoading = true
                    let formData = Object.assign(this.model, values)
                    formData.taskId = '-1'
                    formData.templateId = 'exclusive_leave'
                    //formData.handleUsers = 'qcy'
                    request({url: this.url.commitForm, method: 'post', data: formData}).then(res => {
                        if(res.success){
                            this.model = res.result 
                            this.$message.success(res.message);
                        } else {
                            notification.error({ message: '系统提示', description: res.message || "执行出现错误"})
                        }
                    }).finally(() => {
                        this.confirmLoading = false
                    })
                }
            })
        },
        saveData() {
            
            this.$refs.leaveTestForm.getForm().validateFields((err, values) => {
                if (!err) {
                    this.confirmLoading = true
                    let formData = Object.assign(this.model, values)
                    request({url: this.url.saveForm, method: 'post', data: formData}).then(res => {
                        if(res.success){
                            this.model = res.result 
                            this.$message.success(res.message);
                        } else {
                            notification.error({ message: '系统提示', description: res.message || "执行出现错误"})
                        }
                    }).finally(() => {
                        this.confirmLoading = false
                    })
                }
            })
        },
        openCommit() {
            this.$refs.leaveTestForm.getForm().validateFields((err, values) => {
                if (!err) {
                    let formData = Object.assign(this.model, values)
                    this.$refs['addLeaveCommit'].open(formData)
                }
            })
            
        },
        commitForm(data) {
            let param = {}
            param = Object.assign({}, param, this.model)
            param.templateId = this.taskInfo.templateId
            param.taskId = this.taskInfo.id
            if (data) {
                param = Object.assign({}, param, data)
            }
            //param.approvalOpinion = '通过'
            // this.confirmLoading = true
            // request({url: this.url.commitForm, method: 'post', data:param}).then(res => {
            //     if(res.success){
            //         this.formData = res.result
            //         this.$message.success(res.message);
            //     } else {
            //         notification.error({ message: '系统提示', description: res.message || "执行出现错误"})
            //     }
            // }).finally(() => {
            //     this.confirmLoading = false
            // })
        },
        emitChangeHandler(changeItem) {
            this.$emit('handlerChange',changeItem);
        },
        emitClickHandler(field,form) {
            this.$emit('handlerClick',field,form);
        },
        cancel() {
            this.visible = false
            this.$emit('handleClose');
        }
    }
}
</script>

<style scoped>
  .drawer-bootom-button {
    position: absolute;
    bottom: 0;
    width: 100%;
    border-top: 1px solid #e8e8e8;
    padding: 10px 16px;
    text-align: right;
    left: 0;
    background: #fff;
    border-radius: 0 0 2px 2px;
  }
</style>
